<?php
/**
 * Free tokens experiment
 * @see http://173.45.224.86:9003/pcgsnwiki/index.php/Experiments#Free_Tokens_Per_Time
 */ 
class Nutri_Products_InMenu
{       
        /**
         * 
         * @var BJBPO_Model_Product
         */
        protected $_menu;
        
        public function __construct($idMenu)
        {
                $this->_menu = $idMenu;
        }
        
        public function getInMenues()
        {
                $productMenu = new Nutri_Model_ProductMenu();
                if(!empty($this->_menu)) {
                        $resultProductMenu = $productMenu->fetchAll(
                                $productMenu->select()
                                                        ->where("menu_id = ?", $this->_menu)
                        );
                        
                        $multiOptionsSelected = array();
                        foreach ($resultProductMenu as $itemPIM) {
                        $multiOptionsSelected[] = $itemPIM->getProduct()->getId();
                        }

                        return $multiOptionsSelected;
                }
        }
        
        
        public function setInMenues($menuesSeted, $idMenu)
        {
        		if(!empty($idMenu)){
	                $prodMenu = new Nutri_Model_ProductMenu();
	                $resultProdMenu = $prodMenu->fetchAll(
	                                $prodMenu->select()
	                                        ->where('menu_id = ?', $idMenu)
	                );
	                
	                foreach ($resultProdMenu as $itemPM) {
	                        $productId = $itemPM->getProduct()->getId();
	                        $this->deleteRowProductMenu($productId);
	                }
	                
	                
	                $db = Zend_Db_Table::getDefaultAdapter();
	                foreach ($menuesSeted as $itemSetPM) {
	                        $dataToAdd = array();
	                        $dataToAdd['product_id'] = $itemSetPM;
	                        $dataToAdd['menu_id'] = $idMenu;
	                        
	                        $db->insert('product_menu', $dataToAdd);
	                }
        		}
                        
        }
        
        protected function deleteRowProductMenu($id)
        {               
                $db = Zend_Db_Table::getDefaultAdapter();
                $where = $db->quoteInto('product_id = ?', $id);
                $db->delete('product_menu', $where);
        }
}